//***********************************************************************
//	File:		WoodenCrate.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************



#include "WoodenCrate.h"
#include "ConcussionBlast.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../GameStates/Play.h"

WoodenCrate::WoodenCrate(float posX, float posY) : SGD::Listener(this)
{
	SetAcceleration({ 0 , 0 });
	SetDynamicFrictionCoefficient(.4f); 
	SetHealth(200); 
	//SetImage(); 
	SetIsFrozen(false); 
	SetMass(10); 
	SetMaxHealth(200); 
	SetMaxSpeed(100); 
	SetPassable(false); 
	SetStaticFrictionCoefficient(.7f);
	SetPosition(SGD::Point{ posX, posY }); 
	SetRotation(0);
	SetSpeed(0);
	SetSize ( { 64.0f , 64.0f } );
	//SetTakeDamage(); //particle effect
	SetVelocity(SGD::Vector{ 0, 0 }); 
	RegisterForEvent("CONCUSSION_BLAST");

}

WoodenCrate::~WoodenCrate()
{
	UnregisterFromEvent("CONCUSSION_BLAST");
}
void WoodenCrate::Update(float dt)
{
	//Final line of function
	if(GetForce().x != 0.0f || GetForce().y != 0.0f)
		Moveable::Update(dt);
	else
	{
		SetVelocity({0.0f, 0.0f});
	}
	
}


void WoodenCrate::Render()
{
	SGD::GraphicsManager* draw = 
		SGD::GraphicsManager::GetInstance();

	SGD::Point cameraPos = Play::GetInstance()->GetCameraPos(); 

	SGD::Point offset = GetPosition() - GetSize() / 2.0f;
	offset.x -= cameraPos.x; 
	offset.y -= cameraPos.y;

	SGD::Rectangle render = GetRect();
	render.left -= cameraPos.x;
	render.right -= cameraPos.x;
	render.top -= cameraPos.y;
	render.bottom -= cameraPos.y;

	

	draw->DrawRectangle(render, {255,255,0,0}, {255, 0,0,0}, 2);
}


void WoodenCrate::HandleCollision(const IEntity* pOther)
{
	
}

void WoodenCrate::HandleEvent(const SGD::Event* pEvent)
{
	if(pEvent->GetEventID() == "CONCUSSION_BLAST")
	{
		ConcussionBlast* blast = reinterpret_cast<ConcussionBlast*>(pEvent->GetSender());
		SGD::Vector distance =  GetPosition() - blast->GetPosition();
		float dist = distance.ComputeLength();
		if ( dist <= blast->GetEffectRadius () )
		{
			SGD::Vector force = distance.ComputeNormalized ();
			force *= blast->GetForce ();
			SetForce ( force );
		}
		
	}
}

SGD::Rectangle WoodenCrate::GetRect() const
{
	SGD::Rectangle rect;
	
	rect.left = m_ptPosition.x - (m_szSize.width  * 0.5f);
	rect.top = m_ptPosition.y - (m_szSize.height  * 0.5f);
	rect.right = rect.left + m_szSize.width;
	rect.bottom = rect.top + m_szSize.height;

	return rect;
}

void WoodenCrate::ModifyHealth(float hpAmount)
{

}

void WoodenCrate::ApplyFriction(float dt)
{

}


